*This file replicates regression results for Volatility outcomes. 
*The code used in this file is adapted from Baker, Bloom, and Davis (2016) to generate results related to environmental policy uncertainty.

set matsize 5000
clear all
set more off
use data/final_data.dta

so year month 
merge 1:1 year month using data/modern_epu_data_clean
ren UP PU
gen eEPU=EPU/E
gen pEPU=EPU/P
gen uEPU=EPU/U
gen euEPU=EPU/EU
gen puEPU=EPU/PU
gen epEPU=EPU/EP
gen gE_P_U=(E*P*U)^(1/3)
gen aE_P_U=(E+P+U)/3
global unc="E P U EP EU PU EPU eEPU pEPU uEPU euEPU puEPU epEPU _EPU gE_P_U aE_P_U"
global lunc="lE lP lU lEP lEU lPU lEPU leEPU lpEPU luEPU leuEPU lpuEPU lepEPU l_EPU lgE_P_U laE_P_U"
foreach var in $unc {
gen l`var'=log(`var')
}
gen ym=year+(month-1)/12
gen yq=year+(floor((month-1)/3))/4
drop _m

so year month
merge m:1 year month using data/categories  

gen quarter = 1
replace quarter = 2 if month>3
replace quarter = 3 if month>6
replace quarter =4 if month>9
lab var uncert "Combined EPU index"
lab var EPU "News only index"
collapse uncert - PCF $unc $lunc defense health fin_regulation  epu yq ym, by(year quarter)
compress
save data/epu_data, replace 
clear all

use data/volandcapex_quarterly.dta,clear

cap destring, replace
cap gen date = dofm(datadate)
cap gen quarter = quarter(date)
cap gen year = year(date)
cap gen month = month(date)

**These two lines below move from old to new data
cap merge 1:1 gvkey year quarter using  data/volandcapex_quarterly_updated.dta,update
cap tab _m
cap drop _m

***Note master data only has back until 1995
cap merge m:1 year quarter gvkey using data/all_firm_data_updated.dta
cap drop *uncert*
drop if _merge==2
drop _merge
merge m:1 year quarter using data/epu_data.dta //
drop if _merge==2
drop _merge

********************Adding Volume Data
rename cusip cusip
merge m:1 cusip year quarter using data/quarterly_volume_data.dta
drop if _merge==2
drop _merge
cap order gvkey ticker* cusip* permno year quarter month datadate date datafdate
drop min_quarter 
sort gvkey year quarter
tsset gvkey period_code
gen luncert=log(uncert)

********Adding beta and 10k data
so sic3
merge m:1 sic3 using data/sic3_betas
drop if _m==2
drop _m

so gvkey
merge m:1 gvkey using data/steve10k_gvkey
winsor2 jp,replace cuts(2 98)
replace jp=log(jp)
drop if _m==2
drop _m

********************Updating/Fixing VIX
ren vix oldvix
so yq
merge m:1 yq using data/vix.dta,update
replace lvix=log(vix)

*Generate a bunch of uncertainty interactions with prefered intensity measure, giving them the same SD as the baseline
foreach var of varlist h_firm_sic_intens {
	foreach var2 of varlist $unc $lunc uncert vix luncert lvix fed fed_fore {
		qui gen `var2'_X_`var' = `var2'*`var'
	}
}

*Test out with other intensity measures 
egen sdXbaseline=sd(h_firm_sic_intens)
foreach var of varlist sic_intens belo_intens firm_intens firm_sic_intens beta jp {
	egen sdX`var'=sd(`var')
	replace `var'=`var'*sdXbaseline/sdX`var'
	qui	gen luncert_X_`var'_intens = luncert*`var'
	qui	gen lEPU_X_`var'_intens = lEPU*`var'
	qui	gen fed_X_`var'_intens = fed*`var'
}

gen lavg_vol_91  = log(avg_vol_91)
gen lend_vol_91  = log(end_vol_91)
gen lavg_vol_30  = log(avg_vol_30)
gen lend_vol_30  = log(end_vol_30)
gen lavg_vol_182  = log(avg_vol_182)
gen lend_vol_182  = log(end_vol_182)
gen lavg_vol_365  = log(avg_vol_365)
gen lend_vol_365  = log(end_vol_365)
cap gen lavg_volr  = log(avg_volr)
cap gen lend_volr  = log(end_volr)
tsset
gen l_luncert_X_h_firm_sic_intens=l.luncert_X_h_firm_sic_intens
gen f_luncert_X_h_firm_sic_intens=f.luncert_X_h_firm_sic_intens
cap label var l_luncert_X_h_firm_sic_intens "Lag Uncert*Firm Intens"
cap label var f_luncert_X_h_firm_sic_intens "Fwrd Uncert*Firm Intens"
cap label var luncert_X_h_firm_sic_intens "Log(Uncert)*Firm Intens"
cap label var fed_X_h_firm_sic_intens "Fed Purchases*Firm Intens"
cap label var fed_fore_X_h_firm_sic_intens "Frcst Fed Purchases*Firm Intens"

cap destring, replace
compress

*Missing lavg_volr in monthly data
cap gen lavg_volr=lavg_vol_91

*Cleaning of volatilty for some extreme outliers (either mistakes or stocks not normally traded)
cap gen ratio_91=avg_vol_91/avg_volr
cap replace lavg_vol_91=. if ratio_91<3|ratio_91>100
foreach var in lavg_vol_30 lavg_vol_91 lavg_vol_182 lavg_volr {
*replace `var'=. if exp(`var')>1.5|exp(`var')<0.1
su `var',de
replace `var'=r(p1) if `var'<r(p1)&`var'~=.
replace `var'=r(p99) if `var'>r(p99)&`var'~=.
}

*For firm size interacting with env (This is our addition to BBD's code)
gen sizeemp=emp
replace sizeemp=round(1000*sizeemp)
gen logemp=log(sizeemp)

*For weighting purposes useful
egen memp=mean(emp)
replace emp=memp if emp==.
egen mtemp=mean(emp),by(ticker_code)
replace emp=round(1000*mtemp) 
replace emp=1 if emp==0
egen mrev=mean(rev),by(ticker_code)
replace mrev=round(mrev)
replace mrev=1 if mrev==0
egen mmrev=mean(mrev)
replace mrev=round(mmrev) if mrev==.

*Drop without volume
drop if volume==0|volume==.

*Can get volume results if censor/windosrize on volume
gen lvolume=log(1+volume)
su lvolume,de
replace lvolume=. if lvolume<r(p1)
replace lvolume=r(p99) if lvolume>r(p99)&lvolume~=.

compress
cap log close
cap log using "volatility",t replace

*******************BBD's original code (except lines 147 - 150) ends here******************************************
**************************************************************************************************************************

gen SIC2=int(sic4/100)
*Merge Environment and other relevant data
drop _m
merge m:1 year quarter using data/04_Env_Policy_indices_qtr
drop _m
merge m:1 year quarter using data/12_EPU_EPAregions_Quarter
drop _m
merge m:1 year quarter using data/13_EP_EPAregions_Quarter
drop _m
drop if gvkey ==.
merge m:m gvkey using data/gv_key_state
drop _m
drop if year==.
merge m:1 year quarter using data/A_DP_FP_HP_Quarter
drop _m
merge m:1 year using data/14_year_DividedGovt
ren ( DividedGovDeffinition1 DividedGovDeffinition2 ) (div1 div2)

*Create variables to capture EPA regions specific EnvP and EnvPU
foreach var in PU P {
gen E`var'_EPAreg2 = E`var'_NB_local
replace E`var'_EPAreg2 = Env`var'_EPA_1_2 if inlist(state, "VT", "NH", "ME", "MA", "RI", "CT", "NJ", "NY")
replace E`var'_EPAreg2 = Env`var'_EPA_3_4 if inlist(state, "DE", "MD", "PA" , "VA", "WV")
replace E`var'_EPAreg2 = Env`var'_EPA_3_4 if inlist(state, "AL" , "FL" , "GA" , "KY" , "MS" , "NC" , "SC" , "TN")
replace E`var'_EPAreg2 = Env`var'_EPA_5_7 if inlist(state, "IL" , "IN" , "MI" , "MN" , "OH" , "WI") 
replace E`var'_EPAreg2 = Env`var'_EPA_5_7 if inlist(state, "IA" , "KS" , "MO" , "NE")
replace E`var'_EPAreg2 = Env`var'_EPA_6_8 if inlist(state, "AR", "LA" , "NM" , "OK" , "TX" ) 
replace E`var'_EPAreg2 = Env`var'_EPA_6_8 if inlist(state, "CO" , "MT" , "ND" , "SD" , "UT" , "WY")
replace E`var'_EPAreg2 = Env`var'_EPA_9_10 if inlist(state, "AZ" , "CA" , "HI" , "NV", "AK" , "ID" , "OR" , "WA")
}
*
********************************************************
*Create political party period indicators
*If senate year
gen repub=1 if (year>1980 & year<1987)|(year>1994 & year<2001)|(year>2002 & year<2007)|(year>2014)
replace repub=0 if repub==.

*If president year
gen repub_presi= (year>=1981 & year<1993) | (year>=2001 & year<2009) | (year>=2017 & year<2021) 

*Gen firm type indicators
gen env_firms=1 if inlist(sic3, 104, 109, 122, 131, 138, 241, 242, 491, 492) | inlist(SIC2, 1, 2, 37)
replace env_firms=0 if env_firms==.
foreach var in EPU_Factiva EP_Factiva EPU_NB EP_NB EPU_NB_local EP_NB_local EPU_EPAreg2 EP_EPAreg2{
replace `var'=0 if env_firms==0
}
gen def_firms = inlist(sic3, 348, 381, 871) 
foreach var in dp dpu {
replace `var'=0 if def_firms==0
}

gen health_firms=(sic3>=800&sic3<=809)
foreach var in health hp hpu{
replace `var'=0 if health_firms==0
}
gen fin_firms= (sic3>=600&sic3<=679) 
foreach var in fin_regulation fp fpu{
replace `var'=0 if fin_firms==0
}

*Generate variables for regression
foreach var in EPU_Factiva EP_Factiva EPU_NB EP_NB EPU_NB_local EP_NB_local EPU_EPAreg2 EP_EPAreg2 dp dpu hp hpu fp fpu {
gen l`var'=log(1+`var')
}
foreach var in lEPU_Factiva lEP_Factiva lEPU_NB lEP_NB ldp ldpu lhp lhpu lfp lfpu{
foreach y in repub repub_presi div1 div2{
gen `var'X`y'=`var'*`y'
}
}

foreach var in lEPU_Factiva lEPU_NB lEPU_NB_local lEP_Factiva lEP_NB lEP_NB_local ldp ldpu lhp lhpu lfp lfpu{
foreach y in logemp lrevtq{
gen `var'_X_`y'= `var'*`y'
}
}
gen mining = ((sic3>=101 & sic3<=109) | sic3==122 | sic3==123 | sic3==124)
gen utilities = (sic3==491 | sic3==492)
gen agri = (SIC2==1 | SIC2==2 | sic3==241 | sic3==242)
gen vehicle = (SIC2==37)
gen oilgas = (sic3>=131 & sic3<=138)

foreach var in lEPU_Factiva lEP_Factiva lEPU_NB lEP_NB lEPU_NB_local lEP_NB_local {
foreach sec in mining utilities agri oilgas{
gen `var'_X_`sec'= `var'*`sec'
}
}
*

*Summary statistics (Table 3)
preserve
qui reghdfe lavg_vol_30 lEPU_NB ldpu lhpu lfpu lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens  [aw=emp], ab(ticker_code period_code) vce(cl ticker_code)
keep if e(sample)
estpost sum avg_vol_30
estpost sum avg_vol_30 if env_firms==1
estpost sum avg_vol_30 if def_firms==1
estpost sum avg_vol_30 if health_firms==1
estpost sum avg_vol_30 if fin_firms==1
restore

**********REGRESSIONS*******************************************************************

****** With PUs and Ps (Table 5)
reghdfe lavg_vol_30 lEPU_NB lEP_NB ldpu ldp lhpu lhp lfpu lfp lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens   [aw=emp], ab(ticker_code period_code) vce(cl ticker_code)

****** Original only with PUs (Table A3)
reghdfe lavg_vol_30 lEPU_NB ldpu lhpu lfpu lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens   [aw=emp], ab(ticker_code period_code) vce(cl ticker_code)

****** Heterogeneity: Firm size (Table 6 and A7)
reghdfe lavg_vol_30 lEPU_NB_X_lrevtq lEPU_NB lEP_NB_X_lrevtq lEP_NB ldpu_X_lrevtq ldpu ldp_X_lrevtq ldp lhpu_X_lrevtq lhpu lhp_X_lrevtq lhp lfpu_X_lrevtq lfpu lfp_X_lrevtq lfp lrevtq lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens [aw=emp], ab(ticker_code period_code) vce(cl ticker_code)
reghdfe lavg_vol_30 lEPU_NB_X_logemp lEPU_NB lEP_NB_X_logemp lEP_NB ldpu_X_logemp ldpu ldp_X_logemp ldp lhpu_X_logemp lhpu lhp_X_logemp lhp lfpu_X_logemp lfpu lfp_X_logemp lfp logemp lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens, ab(ticker_code period_code) vce(cl ticker_code)

****** Heterogeneity: Political party (Tables 7, 8 and A9)
reghdfe lavg_vol_30 lEPU_NBXrepub_presi lEPU_NB lEP_NBXrepub_presi  lEP_NB ldpuXrepub_presi ldpu ldpXrepub_presi ldp lhpuXrepub_presi lhpu lhpXrepub_presi lhp lfpuXrepub_presi lfpu lfpXrepub_presi lfp repub_presi lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens [aw=emp], ab(ticker_code period_code) vce(cl ticker_code)
reghdfe lavg_vol_30 lEPU_NBXdiv2 lEPU_NB lEP_NBXdiv2  lEP_NB ldpuXdiv2 ldpu ldpXdiv2 ldp lhpuXdiv2 lhpu lhpXdiv2 lhp lfpuXdiv2 lfpu lfpXdiv2 lfp div2 lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens [aw=emp], ab(ticker_code period_code) vce(cl ticker_code)
reghdfe lavg_vol_30 lEPU_NBXrepub lEPU_NB lEP_NBXrepub lEP_NB ldpuXrepub ldpu ldpXrepub ldp lhpuXrepub lhpu lhpXrepub lhp lfpuXrepub lfpu lfpXrepub lfp repub lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens [aw=emp], ab(ticker_code period_code) vce(cl ticker_code)

****** Heterogeneity: Firm sector (Table A8)
reghdfe lavg_vol_30 lEPU_NB_X_mining lEPU_NB_X_utilities lEPU_NB_X_agri lEPU_NB_X_oilgas lEPU_NB lEP_NB_X_mining lEP_NB_X_utilities lEP_NB_X_agri lEP_NB_X_oilgas lEP_NB ldpu ldp lhpu lhp lfpu lfp lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens [aw=emp], ab(ticker_code period_code) vce(cl ticker_code)

****** Local EnvPU effect using EPA region clusters as local (Table A10)
reghdfe lavg_vol_30 lEPU_EPAreg2 lEP_EPAreg2 ldpu ldp lhpu lhp lfpu lfp lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens   [aw=emp] if year>=1990, ab(ticker_code period_code) vce(cl ticker_code)

******* National EnvPU effect  (Table A11)
reghdfe lavg_vol_30 lEPU_Factiva lEP_Factiva ldpu ldp lhpu lhp lfpu lfp lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens   [aw=emp], ab(ticker_code period_code) vce(cl ticker_code)

****** Local and national news effect on firm performance (Table A12) 
reghdfe lavg_vol_30 lEPU_Factiva lEP_Factiva lEPU_EPAreg2 lEP_EPAreg2 ldpu ldp lhpu lhp lfpu lfp lEPU_X_h_firm_sic_intens fed_X_h_firm_sic_intens   [aw=emp] if year>=1990, ab(ticker_code period_code) vce(cl ticker_code)


*****************************************************************************************************

